package com.zfzs.post.modules.address.service.impl;

import com.zfzs.post.modules.address.domain.AgentArea;
import com.zfzs.post.modules.address.repository.AgentAreaRepository;
import com.zfzs.post.modules.address.service.AgentAreaService;
import com.zfzs.post.modules.address.service.SysCityService;
import com.zfzs.post.modules.address.service.dto.AgentAreaDTO;
import com.zfzs.post.modules.address.service.dto.AgentAreaQueryCriteria;
import com.zfzs.post.modules.address.service.dto.SysCityDTO;
import com.zfzs.post.modules.address.service.mapper.AgentAreaMapper;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;

/**
* @author ck
* @date 2019-09-09
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AgentAreaServiceImpl implements AgentAreaService {

    @Autowired
    private AgentAreaRepository agentAreaRepository;

    @Autowired
    private AgentAreaMapper agentAreaMapper;

    @Autowired
    private SysCityService sysCityService;

    @Override
    public Object queryAll(AgentAreaQueryCriteria criteria, Pageable pageable){
        Page<AgentArea> page = agentAreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agentAreaMapper::toDto));
    }

    @Override
    public Object queryAll(AgentAreaQueryCriteria criteria){
        return agentAreaMapper.toDto(agentAreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public AgentArea findById(Long id) {
        Optional<AgentArea> agentArea = agentAreaRepository.findById(id);
        ValidationUtil.isNull(agentArea,"AgentArea","id",id);
        return agentArea.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgentArea create(AgentArea resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        //查询省 市 区 中文
        SysCityDTO province = sysCityService.findById(resources.getProvince());
        SysCityDTO city = sysCityService.findById(resources.getCity());
        SysCityDTO area = sysCityService.findById(resources.getArea());
        resources.setAreaId(snowflake.nextId());
        resources.setCityName(city.getCname());
        resources.setAreaName(area.getCname());
        resources.setProvinceName(province.getCname());
        resources.setIsDel(false);
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return agentAreaRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgentArea resources) {
        Optional<AgentArea> optionalAgentArea = agentAreaRepository.findById(resources.getAreaId());
        ValidationUtil.isNull( optionalAgentArea,"AgentArea","id",resources.getAreaId());
        AgentArea agentArea = optionalAgentArea.get();
        //查询省 市 区 中文
        SysCityDTO province = sysCityService.findById(resources.getProvince());
        SysCityDTO city = sysCityService.findById(resources.getCity());
        SysCityDTO area = sysCityService.findById(resources.getArea());
        resources.setCityName(city.getCname());
        resources.setAreaName(area.getCname());
        resources.setProvinceName(province.getCname());
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        agentArea.copy(resources);
        agentAreaRepository.save(agentArea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        agentAreaRepository.deleteById(id);
    }

    @Override
    public void deleteAreaById(Long id) {
        agentAreaRepository.deleteAreaById(id);
    }

    @Override
    public void cancelDefaultByAgentId(Long agentId) {
        agentAreaRepository.cancelDefaultByAgentId(agentId);
    }

    @Override
    public void defaultAreaById(Long id) {
        agentAreaRepository.defaultAreaById(id);
    }

    @Override
    public void cancelDefaultById(Long areaId) {
        agentAreaRepository.cancelDefaultById(areaId);
    }

    @Override
    public List<AgentArea> queryAreaList(Long agentId) {
        return agentAreaRepository.queryAreaList(agentId);
    }

    @Override
    public AgentArea getDefault(Long agentId) {
        return agentAreaRepository.findDefaultArea(agentId);
    }
}